﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class GroupCard : MonoBehaviour {

	private Text TimeLabel;
	private Image Success;
	private Image Scroll;
	private Image HeadType;
	private Image MiddleType;
	private Image TailType;
	private int HeadTypeInt;
	private int MiddleTypeInt;
	private int TailTypeInt;

	private Image ClearHeadType;
	private Image ClearMiddleType;
	private Image ClearTailType;

	private ZjhPlayer player = null;
	private BetBtnControl btns = null;
	private GroupTool tool = new GroupTool();
	private List<GameObject> cells = new List<GameObject> ();
	public static List<int> UsedCard = new List<int> ();
	public static List<ZJHCards> tou = new List<ZJHCards> ();
	public static List<ZJHCards> zhong = new List<ZJHCards> ();
	public static List<ZJHCards> wei = new List<ZJHCards> ();
	public static bool GroupOver = false;
	public static bool Open = false;

	void Start () {
		
		TimeLabel = transform.GetChild (2).GetComponent<Text> ();
		Success = transform.GetChild (3).GetComponent<Image> ();
		Scroll = transform.GetChild (5).GetComponent<Image> ();
		HeadType = transform.GetChild (6).GetComponent<Image> ();
		MiddleType = transform.GetChild (7).GetComponent<Image> ();
		TailType = transform.GetChild (8).GetComponent<Image> ();

		ClearHeadType = transform.GetChild (9).GetComponent<Image> ();
		ClearMiddleType = transform.GetChild (10).GetComponent<Image> ();
		ClearTailType = transform.GetChild (11).GetComponent<Image> ();

		for( int i = 0 ; i < 3 ; i++ ){
			tou.Add (transform.Find ("tou").GetChild (i).GetComponent<ZJHCards> ());
		}

		for( int i = 0 ; i < 5 ; i++ ){
			zhong.Add (transform.Find ("zhong").GetChild (i).GetComponent<ZJHCards> ());
		}

		for( int i = 0 ; i < 5 ; i++ ){
			wei.Add (transform.Find ("wei").GetChild (i).GetComponent<ZJHCards> ());
		}

		EventTriggerListener.Get (Success.transform.GetChild (0).gameObject).onClick = ResetCards;
		EventTriggerListener.Get (Success.transform.GetChild (1).gameObject).onClick = StartCompare;

		EventTriggerListener.Get (ClearHeadType.gameObject).onClick = ClearGroup;
		EventTriggerListener.Get (ClearMiddleType.gameObject).onClick = ClearGroup;
		EventTriggerListener.Get (ClearTailType.gameObject).onClick = ClearGroup;

		EventTriggerListener.Get (transform.Find ("tou").gameObject).onClick = ClickHead;
		EventTriggerListener.Get (transform.Find ("zhong").gameObject).onClick = ClickMiddle;
		EventTriggerListener.Get (transform.Find ("wei").gameObject).onClick = ClickTail;
	}

	private void ClickHead(GameObject go){

		if ( tou [0].getValue () == -1 ) {

			List<ZJHCards> templist = new List<ZJHCards> ();
			foreach(ZJHCards z in player.cardList){
				if ( z.getSelect () ) {
					templist.Add (z);
				}
			}

			if(templist.Count == 3){

				for( int i = 0 ; i < tou.Count ; i++ ){
					if ( tou [i].getValue() == -1 ) {
						tou [i].setValue(templist[i].getValue());
						tou [i].ChangeStyle (templist [i].getValue().ToString());
					}
				}

				HeadTypeInt = ChangeType(HeadType,Sort (tou));

				foreach(ZJHCards z in templist){
					z.setSelect ();
					UsedCard.Add (z.getValue());
				}

				player.setCardUse ();
			}
		}
	}

	private void ClickMiddle(GameObject go){

		if( zhong[0].getValue() == -1 ){

			List<ZJHCards> templist = new List<ZJHCards> ();
			foreach(ZJHCards z in player.cardList){
				if ( z.getSelect () ) {
					templist.Add (z);
				}
			}

			if(templist.Count == 5){

				for (int i = 0; i < wei.Count; i++) {
					if (zhong [i].getValue () == -1) {
						zhong [i].setValue (templist [i].getValue ());
						zhong [i].ChangeStyle (templist [i].getValue ().ToString ());
					}
				}

				MiddleTypeInt = ChangeType(MiddleType,Sort (wei));
				foreach(ZJHCards z in templist){
					z.setSelect ();
					UsedCard.Add (z.getValue());
				}

				player.setCardUse ();

			}
		}
	}

	private void ClickTail(GameObject go){

		if( wei[0].getValue() == -1 ){

			List<ZJHCards> templist = new List<ZJHCards> ();
			foreach(ZJHCards z in player.cardList){
				if ( z.getSelect () ) {
					templist.Add (z);
				}
			}

			if(templist.Count == 5){

				for (int i = 0; i < wei.Count; i++) {
					if (wei [i].getValue () == -1) {
						wei [i].setValue (templist [i].getValue ());
						wei [i].ChangeStyle (templist [i].getValue ().ToString ());
					}
				}

				TailTypeInt = ChangeType(TailType,Sort (wei));

				foreach(ZJHCards z in templist){
					z.setSelect ();
					UsedCard.Add (z.getValue());
				}

				player.setCardUse ();

			}
		}
	}

	private void Filter(){

		foreach ( GameObject g in cells ) {
			Destroy (g);
		}
		
		List<List<Combination>> overlist = tool.GroudCard (player.list);
		List<List<Combination>> resultList = new List<List<Combination>> ();

		for( int i = 8 ; i > -1 ; i -- ){
			for( int z = 0 ; z < overlist.Count ; z ++ ){
				if (overlist [z] [0].Type == i) {
					resultList.Add (overlist [z]);
				}
			}
		}
			
		for( int p = 0 ; p < resultList.Count ; p++ ){
			if (p > 5) {
				break;
			}
			GameObject prefabs = transform.GetChild(5).Find ("GroupCell").gameObject;
			GameObject go = (GameObject)GameObject.Instantiate (prefabs,Scroll.transform);

			go.GetComponent<GroupCell> ().setCombination (resultList[p]);
			go.transform.localPosition = new Vector3 (0, 145 - 60 * p - p * 5, 0);

			EventTriggerListener.Get (go).onClick = SelectCombin;
			cells.Add (go);
		}
	}


	private void SelectCombin(GameObject go){

		List<Combination> list = go.GetComponent<GroupCell> ().getCombination ();
		UsedCard = new List<int> ();

		for( int i = 0 ; i < list[0].getValue().Count ; i++ ){
			
			wei [i].setValue(list [0].getValue () [i]);
			wei [i].ChangeStyle (list [0].getValue () [i].ToString ());
			UsedCard.Add (list [0].getValue () [i]);
		}
		TailTypeInt = ChangeType(TailType,Sort (wei));

		for( int i = 0 ; i < list[1].getValue().Count ; i++ ){

			zhong [i].setValue(list [1].getValue () [i]);
			zhong [i].ChangeStyle (list [1].getValue () [i].ToString ());
			UsedCard.Add (list [1].getValue () [i]);
		}
		MiddleTypeInt = ChangeType(MiddleType,Sort (zhong));
		for( int i = 0 ; i < list[2].getValue().Count ; i++ ){

			tou [i].setValue(list [2].getValue () [i]);
			tou [i].ChangeStyle (list [2].getValue () [i].ToString ());
			UsedCard.Add (list [2].getValue () [i]);
		}

		HeadTypeInt = ChangeType(HeadType,Sort (tou));
		player.setCardUse ();

	}

	public void SetPlayer( ZjhPlayer value ){
		player = value;
		player.setCallBack (PickCard);
	}

	public void SetBtn( BetBtnControl value ){
		btns = value;
	}

	private void ResetCards( GameObject go ){

	}

	private void StartCompare( GameObject go ){
		List<Combination> Msg = new List<Combination> ();
		Msg.Add(new Combination ());
		Msg.Add(new Combination ());
		Msg.Add(new Combination ());

		Msg [0].Type = TailTypeInt;
		for( int i = 0 ; i < wei.Count ; i++ ){
			Msg [0].getValue().Add (wei [i].getValue ());
		}

		Msg [1] = new Combination ();
		Msg [1].Type = MiddleTypeInt;
		for( int i = 0 ; i < zhong.Count ; i++ ){
			Msg [1].getValue().Add (zhong [i].getValue ());
		}

		Msg [2] = new Combination ();
		Msg [2].Type = HeadTypeInt;
		for( int i = 0 ; i < tou.Count ; i++ ){
			Msg [2].getValue().Add (tou [i].getValue ());
		}

		GSocket.Send(tools.ByteFactory.makeByte(621115,Msg));
	}

	private void ClearGroup( GameObject go ){
		List<int> l = new List<int> ();
		if ( go == ClearHeadType.gameObject ) {
			for( int i = 0 ; i < tou.Count ; i++ ){
				if( tou [i].getValue() != -1 ){
					l.Add (tou [i].getValue());
				}
				tou [i].setValue(-1);
			}
			HeadType.gameObject.SetActive(false);
		}

		if ( go == ClearMiddleType.gameObject ) {
			for( int i = 0 ; i < zhong.Count ; i++ ){
				if( zhong [i].getValue() != -1 ){
					l.Add (zhong [i].getValue());
				}
				zhong [i].setValue(-1);
			}

			MiddleType.gameObject.SetActive(false);
		}

		if ( go == ClearTailType.gameObject ) {
			for( int i = 0 ; i < wei.Count ; i++ ){
				if( wei [i].getValue() != -1 ){
					l.Add (wei [i].getValue());
				}
				wei [i].setValue(-1);
			}

			TailType.gameObject.SetActive(false);
		}

		for( int q = 0 ; q < l.Count ; q++){
			UsedCard.Remove (l [q]);
		}

		player.deCardUse (l);
	}

	public void CheckSpecial(){
		
		List<List<int>> ThreeStraightAndSameColor = tool.TryThreeStraightAndSameColor (new List<int>(player.list.ToArray()));
		if ( ThreeStraightAndSameColor != null ) {
			ZJHDealer.Licening = false;
			return;
		}

		List<List<int>> ThreeSameColor = tool.TryThreeSameColor (player.list);
		if ( ThreeSameColor != null ) {
			ZJHDealer.Licening = false;
			return;
		}

		List<List<int>> ThreeStraight = tool.TryThreeStraight (new List<int>(player.list.ToArray()));
		if ( ThreeStraight != null ) {
			ZJHDealer.Licening = false;
			return;
		}

		List<int> SixAndHalf = tool.TrySixAndHalf (new List<int>(player.list.ToArray()));
		if ( SixAndHalf != null ) {
			ZJHDealer.Licening = false;
			return;
		}

		HeadType.gameObject.SetActive(true);
		MiddleType.gameObject.SetActive(true);
		TailType.gameObject.SetActive(true);

		Filter ();
	}

	private List<int> getList(List<ZJHCards> value){
		List<int> l = new List<int> ();
		for( int i = 0 ; i < value.Count ; i++ ){
			l.Add (value [i].getValue ());
		}
		return l;
	}

	private int Sort( List<ZJHCards> value ){

		List<int> l = getList (value);

		if ( value.Count > 3 ) {

			if ( tool.GetStraghtColor (getList (value)) != null ) {
				l.Sort ();
				for( int i = 0 ; i < value.Count ; i++ ){
					value [i].setValue (l [i]);
					value [i].ChangeStyle (l [i].ToString());
				}
				return Combination.StraightColor;
			}

			Combination Branch = tool.GetBranch (getList (value));
			if ( Branch != null ) {
				for( int i = 0 ; i < value.Count ; i++ ){
					value [i].setValue (Branch.getValue()[i]);
					value [i].ChangeStyle (Branch.getValue()[i].ToString());
				}
				return Combination.Branch;
			}

			Combination Gourds = tool.GetGourds (getList (value));
			if ( Gourds != null ) {
				for( int i = 0 ; i < value.Count ; i++ ){
					value [i].setValue (Gourds.getValue()[i]);
					value [i].ChangeStyle (Gourds.getValue()[i].ToString());
				}
				return Combination.Gourd;
			}

			Combination SameColor = tool.GetSameColors (getList (value));
			if ( SameColor != null ) {
				//l.Clear ();
				for( int i = 0 ; i < value.Count ; i++ ){
					value [i].setValue (l[i]);
					value [i].ChangeStyle (l[i].ToString());
				}
				return Combination.SameColor;
			}

			Combination Straight = tool.GetStraight (getList (value));
			if ( Straight != null ) {
				l.Clear ();
				for( int i = 0 ; i < 14 ; i++ ){
					foreach (ZJHCards z in value) {
						if(!l.Contains(z.getValue()) && z.Value == i ){
							l.Add (z.getValue());
						}
					}
				}

				for( int i = 0 ; i < l.Count ; i++ ){
					value [i].setValue (l[i]);
					value [i].ChangeStyle (l[i].ToString());
				}

				return Combination.Straight;
			}

			Combination DoubleSub = tool.GetDoubleSub (getList (value));
			if ( DoubleSub != null ) {

				for( int i = 0 ; i < DoubleSub.getValue().Count ; i++ ){
					value [i].setValue (DoubleSub.getValue()[i]);
					value [i].ChangeStyle (DoubleSub.getValue()[i].ToString());
				}

				return Combination.DoubleSub;
			}
		}


		Combination Threes = tool.GetThrees (getList (value));
		if ( Threes != null ) {

			l.Clear ();
			l.Add (Threes.getValue () [0]);
			l.Add (Threes.getValue () [1]);
			l.Add (Threes.getValue () [2]);

			for( int i = 0 ; i < 14 ; i++ ){
				foreach (ZJHCards z in value) {
					if(!l.Contains(z.getValue()) &&  z.Value == i ){
						l.Add (z.getValue());
					}
				}
			}

			for( int i = 0 ; i < l.Count ; i++ ){
				value [i].setValue (l[i]);
				value [i].ChangeStyle (l[i].ToString());
			}

			return Combination.Three;
		}



		Combination Sub = tool.GetSub (getList (value));
		if (Sub != null) {

			l.Clear ();
			l.Add (Sub.getValue () [0]);
			l.Add (Sub.getValue () [1]);

			for (int i = 0; i < 14; i++) {
				foreach (ZJHCards z in value) {
					if (!l.Contains (z.getValue ()) && z.Value == i) {
						l.Add (z.getValue ());
					}
				}
			}

			for (int i = 0; i < l.Count; i++) {
				value [i].setValue (l [i]);
				value [i].ChangeStyle (l [i].ToString ());
			}

			return Combination.Sub;

		} else {
			l.Clear ();
			for (int i = 0; i < 14; i++) {
				foreach (ZJHCards z in value) {
					if (!l.Contains (z.getValue ()) && z.Value == i) {
						l.Add (z.getValue ());
					}
				}
			}

			for (int i = 0; i < l.Count; i++) {
				value [i].setValue (l [i]);
				value [i].ChangeStyle (l [i].ToString ());
			}

			return Combination.Chaos;
		}

		return 0;
	}

	private int ChangeType(Image img,int type){
		
		string str = "";
		switch (type) {
		case 0:
			str = "乌龙";
			break;
		case 1:
			str = "对子";
			break;
		case 2:
			str = "两对";
			break;
		case 3:
			str = "三条";
			break;
		case 4:
			str = "顺子";
			break;
		case 5:
			str = "同花";
			break;
		case 6:
			str = "葫芦";
			break;
		case 7:
			str = "炸弹";
			break;
		case 8:
			str = "同花顺";
			break;
		}

		Texture2D texture = (Texture2D)Resources.Load ("image/ZJH/Txt/"+str,typeof(Texture2D));
		if (texture == null) {
			img.sprite = null;
		} else {
			Sprite pic = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
			img.sprite = pic;
		}

		img.gameObject.SetActive (true);
		img.SetNativeSize ();
		return type;
	}

	private bool PickCard( List<ZJHCards> zjhlist ){
		

		return false;
	}

	private void ShowView(){
		transform.localPosition = Vector3.zero;
	}

	private void HideView(){
		transform.localPosition = new Vector3(10000,10000,0);
	}

	// Update is called once per frame
	void Update () {
		
		if ( ZJHDealer.Licening ) {
			ShowView ();
			Open = true;
		} else {
			HideView ();
			Open = false;
		}

		if ( UsedCard.Count == 13 ) {
			GroupOver = true;
		} else {
			GroupOver = false;
		}


		if ( GroupOver ) {
			Success.gameObject.SetActive (true);
		} else {
			Success.gameObject.SetActive (false);
		}
	}
}
